001 /*
002 * Created by IntelliJ IDEA.
003 * User: Wei Wang
004 * Date: 2002-10-17
005 * Time: 17:11:15
006 * To change template for new class use
007 * Code Style | Class Templates options (Tools | IDE Options).
008 */
009 package EVolve.util;
010
011 import EVolve.Scene;
012 import EVolve.visualization.AutoImage;
013 import javax.swing.*;
014 import java.util.*;
015 import java.awt.*;
016 import java.awt.event.*;
017
018 public class ThreadChooser{
019 private ArrayList threadSet;
020 private HashMap imageMap,colorMap;
021 private JDialog dialog;
022 private JList threads;
023 private DefaultListModel threadListModel;
024 private AutoImage mergedImage;
025 private JComboBox comboThread;
026 private int [] indices;
027 private boolean multiChoice;
028 private String thread;
029
030
031 public ThreadChooser(HashMap imageMap,boolean multiChoice) {
032 this.imageMap = imageMap;
033 colorMap = new HashMap();
034 this.multiChoice = multiChoice;
035 }
036
037 private String showMultiChoiceDialog() {
038
039 if (imageMap.size() == 0) return null;
040 dialog = new JDialog(Scene.getFrame(), "Choose thread & color to be visualized", true);
041
042 dialog.setBounds(new Rectangle(250,100));
043
044 threadListModel = new DefaultListModel();
045 threadSet = new ArrayList();
046 threads = new JList(threadListModel);
047 Iterator it = imageMap.keySet().iterator();
048 while (it.hasNext()) {
049 threadSet.add("Thread "+it.next());
050 }
051 for (int i=0; i<threadSet.size(); i++) {
052 String item = (String)threadSet.get(i);
053 threadListModel.addElement(item);
054 }
055 dialog.getContentPane().add(threads,BorderLayout.CENTER);
056
057 JPanel panelButton = new JPanel(new FlowLayout());
058 dialog.getContentPane().add(panelButton, BorderLayout.SOUTH);
059
060 JButton buttonColor = new JButton("Coloring");
061 buttonColor.addActionListener(new ActionListener() {
062 public void actionPerformed(ActionEvent e) {
063 selectColor();
064 }
065 });
066 panelButton.add(buttonColor);
067
068 JButton buttonOK = new JButton("OK");
069 buttonOK.addActionListener(new ActionListener() {
070 public void actionPerformed(ActionEvent e) {
071 if (threads.getSelectedIndices().length == 0) {
072 Scene.showErrorMessage("You must choose at least one thread!");
073 return;
074 }
075 indices = threads.getSelectedIndices();
076 dialog.setVisible(false);
077 dialog = null;
078 }
079 });
080 panelButton.add(buttonOK);
081
082 /*JButton buttonCancel = new JButton("Cancel");
083 buttonCancel.addActionListener(new ActionListener() {
084 public void actionPerformed(ActionEvent e) {
085 dialog.setVisible(false);
086 }
087 });
088 panelButton.add(buttonCancel);*/
089 dialog.pack();
090 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
091 return null;
092 }
093
094 private String showSingleChoiceDialog() {
095 dialog = new JDialog(Scene.getFrame(), "Choose thread to be visualized", true);
096
097 Box boxMain = new Box(BoxLayout.Y_AXIS);
098 dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
099 dialog.setBounds(new Rectangle(250,100));
100 comboThread = new JComboBox();
101 thread = null;
102
103 Iterator it = imageMap.keySet().iterator();
104 while (it.hasNext()) {
105 comboThread.addItem("Thread "+it.next());
106 }
107 boxMain.add(comboThread);
108
109 JPanel panelButton = new JPanel(new FlowLayout());
110 dialog.getContentPane().add(panelButton, BorderLayout.SOUTH);
111
112 JButton buttonOK = new JButton("OK");
113 buttonOK.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e) {
115 if (!validateThread())
116 Scene.showErrorMessage("No data is available for this thread.");
117 else {
118 thread = (String)comboThread.getSelectedItem();
119 dialog.setVisible(false);
120 }
121 }
122 });
123 panelButton.add(buttonOK);
124
125 JButton buttonCancel = new JButton("Cancel");
126 buttonCancel.addActionListener(new ActionListener() {
127 public void actionPerformed(ActionEvent e) {
128 dialog.setVisible(false);
129 }
130 });
131 panelButton.add(buttonCancel);
132 dialog.pack();
133 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight());
134
135 dialog = null;
136 return thread;
137 }
138
139 public String showDialog() {
140 if (multiChoice)
141 return showMultiChoiceDialog();
142 else
143 return showSingleChoiceDialog();
144 }
145
146 private void selectColor() {
147 int index = threads.getSelectedIndex();
148
149 if (index != -1) {
150 Color newColor = JColorChooser.showDialog(Scene.getFrame(), "Choose a color", Color.black);
151 if (newColor != null) {
152 colorMap.put(new Integer(index),newColor);
153 threadListModel.removeAllElements();
154
155 for (int i=0; i<threadSet.size(); i++) {
156 if (colorMap.get(new Integer(i)) == null)
157 threadListModel.addElement(threadSet.get(i));
158 else
159 threadListModel.addElement("<html><font color=#" + getColorHex((Color)colorMap.get(new Integer(i))) + ">"
160 + threadSet.get(i) +" </font></html>" );
161 }
162 }
163 }
164 }
165
166 public AutoImage coloringImages(HashMap imageMap) {
167 mergedImage = new AutoImage();
168 for (int i=0; i<indices.length; i++) {
169 Color newColor = (Color)colorMap.get(new Integer(indices[i]));
170 String threadId = (String)threadSet.get(indices[i]);
171 threadId = threadId.substring(7,threadId.length());
172 AutoImage image = (AutoImage)imageMap.get(new Long(threadId));
173 int w = image.getW();
174 int h = image.getH();
175 for (int j=0; j<w; j++) {
176 for (int k=0; k<h; k++) {
177 if (image.getColor(j,k) != null)
178 if (mergedImage.getColor(j,k) != null)
179 mergedImage.setColor(j,k,new Color(153,0,153));
180 else
181 if (newColor != null) {
182 mergedImage.setColor(j,k,newColor);
183 } else mergedImage.setColor(j,k,image.getColor(j,k));
184 }
185 }
186
187 }
188 return mergedImage;
189 }
190
191 private String getColorHex(Color color) {
192 String returnVal = Integer.toHexString(color.getBlue());
193 if (returnVal.length() < 2) {
194 returnVal = "0" + returnVal;
195 }
196 returnVal = Integer.toHexString(color.getGreen()) + returnVal;
197 if (returnVal.length() < 4) {
198 returnVal = "0" + returnVal;
199 }
200 returnVal = Integer.toHexString(color.getRed()) + returnVal;
201 if (returnVal.length() < 6) {
202 returnVal = "0" + returnVal;
203 }
204
205 return returnVal;
206 }
207
208 public AutoImage getMergedImage() {
209 return mergedImage;
210 }
211
212 private boolean validateThread() {
213 AutoImage img;
214
215 String selected = (String)comboThread.getSelectedItem();
216 img = (AutoImage)imageMap.get(new Long(selected.substring(7,selected.length())));
217 if (img.getW()*img.getH() <= 0) return false;
218
219 return true;
220 }
221
222
223 }